package zhtt;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import zhtt.demo01.Blog;

import java.util.List;

/**
 * Created by zhtt on 2017/4/29.
 */
public class SelectTest {

    @Test
    public void selectBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=sqlSession.selectOne("zhtt.demo01.Blog.selectBlog",1);
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog2(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=sqlSession.selectOne("zhtt.demo01.Blog.selectBlog2",2);
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void selectAllBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            System.out.println(sqlSession);
            List<Blog> blog=sqlSession.selectList("zhtt.demo01.Blog.selectAllBlog");
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void insertBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=new Blog("blog","blog");
            int r=sqlSession.insert("zhtt.demo01.Blog.insertBlog",blog);
            System.out.println(blog);
            System.out.println(r);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void insertBlog2(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=new Blog("blog","blog");
            int r=sqlSession.insert("zhtt.demo01.Blog.insertBlog2",blog);
            System.out.println(r);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void updateBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=new Blog(4,"blog3","blog3");
            int r=sqlSession.update("zhtt.demo01.Blog.updateBlog",blog);
            System.out.println(blog);
            System.out.println(r);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void deleteBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            int r=sqlSession.delete("zhtt.demo01.Blog.deleteBlog",1);
            System.out.println(r);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }
}
